﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Integration;

namespace FennecUI
{
    public abstract class Applet : UserControl
    {
        /// <summary>
        /// Default constructor for ceating Applet objects
        /// </summary>
        /// <param name="appletConfig">AppletConfiguration that defines the Applet properties</param>
        public Applet(AppletConfiguration appletConfig)
        {
            DoubleBuffered = true;
            BackColor = Color.Transparent;
            configuration = appletConfig;

            wpfTitleBarHost = new ElementHost();
            wpfTitleBarHost.BackColorTransparent = true;
            wpfTitleBarHost.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
            wpfTitleBarHost.Width = Width;
            wpfTitleBarHost.Height = 40;
            Controls.Add(wpfTitleBarHost);

            appletTitleBar = new FennecUI.AppletTitleBar();
            appletTitleBar.FullMode = false;
            appletTitleBar.Text = appletConfig.Name;
            wpfTitleBarHost.Child = appletTitleBar;

            AppletContent = new Panel();
            AppletContent.Location = new Point(0, 40);
            AppletContent.Size = new Size(Width, Height - 40);
            AppletContent.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
            Controls.Add(AppletContent);

            appletTitleBar.FullModeBtn.MouseDown += new System.Windows.Input.MouseButtonEventHandler(FullModeBtn_MouseDown);
            appletTitleBar.CloseBtn.MouseDown += new System.Windows.Input.MouseButtonEventHandler(CloseBtn_MouseDown);
        }

        /// <summary>
        /// Closes applet, required to handle the close button
        /// </summary>
        /// <param name="sender">Source object of the event</param>
        /// <param name="e">Event parameters</param>
        void CloseBtn_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            OnClose(this, e);
            Close(this);
        }
        protected abstract void OnClose(Applet sender, EventArgs e);

        void FullModeBtn_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            toggleFullMode();
        }

        /// <summary>
        /// Event delegate for defining Applet specific events
        /// </summary>
        /// <param name="sender">Applet that dispatched the event</param>
        public delegate void AppletEvent(Applet sender);
        
        /// <summary>
        /// Fired when the close button is pressed
        /// Fired after the OnClose method has been called
        /// </summary>
        public event AppletEvent Close;

        private Boolean FullMode;

        /// <summary>
        /// Fired when the applet is about to enter full mode
        /// The onEnterFullMode method is called after this event, to allow it to use the updated size of the applet
        /// </summary>
        public event AppletEvent EnterFullMode;

        /// <summary>
        /// Fired when the applet is about to leave full mode
        /// The onLeaveFullMode method is called after this event, to allow it to use the updated size of the applet
        /// </summary>
        public event AppletEvent LeaveFullMode;

        /// <summary>
        /// Method called after the applet has been resized to full mode, allows applet sub-classes to handle resizing of content
        /// </summary>
        protected abstract void onEnterFullMode();

        /// <summary>
        ///  Method called after the applet has been resized to normal mode, allows applet sub-classes to handle resizing of content
        /// </summary>
        protected abstract void onLeaveFullMode();

        public void toggleFullMode()
        {
            if (!FullMode)
            {
                EnterFullMode(this);
                onEnterFullMode();
                FullMode = true;
                appletTitleBar.FullMode = FullMode;
            }
            else
            {
                LeaveFullMode(this);
                onLeaveFullMode();
                FullMode = false;
                appletTitleBar.FullMode = FullMode;
            }
        }

        private int _staticPos;

        /// <summary>
        /// Used as a refence position for the applet window
        /// When the window group is moved this is used to position the control within the group
        /// This refers only to the x co-ordinate of the applet
        /// </summary>
        public int StaticPosition
        {
            get
            {
                return _staticPos;
            }
            set
            {
                _staticPos = value;
            }
        }

       /// <summary>
       /// Returns the current applet configuration object
       /// </summary>
        public AppletConfiguration Configuration
        {
            get { return configuration; }
        }
        private AppletConfiguration configuration;

        /// <summary>
        /// Called by the Applet parent object, initializes the applet UI
        /// </summary>
        public abstract void StartApplet();

        private ElementHost wpfTitleBarHost;
        protected AppletTitleBar appletTitleBar;
        protected Panel AppletContent;
    }
}
